package com.sddnytsh.body_buildingsystem.services

import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import android.Manifest
import android.util.Log

/**
 * 权限管理类
 * 用于管理华为Health Kit所需的权限
 */
class PermissionManager(private val context: Context) {
    
    private val tag = "PermissionManager"
    
    // 华为Health Kit所需权限
    private val requiredPermissions = listOf(
        Manifest.permission.BLUETOOTH,
        Manifest.permission.BLUETOOTH_ADMIN,
        Manifest.permission.ACCESS_FINE_LOCATION,
        Manifest.permission.ACCESS_COARSE_LOCATION,
        Manifest.permission.BODY_SENSORS,
        Manifest.permission.ACTIVITY_RECOGNITION
    )
    
    /**
     * 检查所有权限是否已授予
     */
    fun areAllPermissionsGranted(): Boolean {
        return requiredPermissions.all { permission ->
            ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED
        }
    }
    
    /**
     * 获取未授予的权限列表
     */
    fun getMissingPermissions(): List<String> {
        return requiredPermissions.filter { permission ->
            ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED
        }
    }
    
    /**
     * 检查特定权限是否已授予
     */
    fun isPermissionGranted(permission: String): Boolean {
        return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED
    }
    
    /**
     * 检查蓝牙权限
     */
    fun isBluetoothPermissionGranted(): Boolean {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            isPermissionGranted(Manifest.permission.BLUETOOTH_CONNECT) &&
            isPermissionGranted(Manifest.permission.BLUETOOTH_SCAN)
        } else {
            isPermissionGranted(Manifest.permission.BLUETOOTH) &&
            isPermissionGranted(Manifest.permission.BLUETOOTH_ADMIN)
        }
    }
    
    /**
     * 检查位置权限
     */
    fun isLocationPermissionGranted(): Boolean {
        return isPermissionGranted(Manifest.permission.ACCESS_FINE_LOCATION) ||
               isPermissionGranted(Manifest.permission.ACCESS_COARSE_LOCATION)
    }
    
    /**
     * 检查身体传感器权限
     */
    fun isBodySensorsPermissionGranted(): Boolean {
        return isPermissionGranted(Manifest.permission.BODY_SENSORS)
    }
    
    /**
     * 检查活动识别权限
     */
    fun isActivityRecognitionPermissionGranted(): Boolean {
        return isPermissionGranted(Manifest.permission.ACTIVITY_RECOGNITION)
    }
    
    /**
     * 获取权限说明文本
     */
    fun getPermissionDescription(permission: String): String {
        return when (permission) {
            Manifest.permission.BLUETOOTH,
            Manifest.permission.BLUETOOTH_ADMIN,
            Manifest.permission.BLUETOOTH_CONNECT,
            Manifest.permission.BLUETOOTH_SCAN -> 
                "需要蓝牙权限来连接华为手表"
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION -> 
                "需要位置权限来获取运动轨迹"
            Manifest.permission.BODY_SENSORS -> 
                "需要身体传感器权限来获取心率等健康数据"
            Manifest.permission.ACTIVITY_RECOGNITION -> 
                "需要活动识别权限来监测运动状态"
            else -> "需要此权限来使用健康功能"
        }
    }
    
    /**
     * 记录权限状态
     */
    fun logPermissionStatus() {
        Log.d(tag, "权限状态检查:")
        requiredPermissions.forEach { permission ->
            val isGranted = isPermissionGranted(permission)
            Log.d(tag, "$permission: ${if (isGranted) "已授予" else "未授予"}")
        }
    }
}
